<%@page import="java.sql.*"%>
<%@page import="com.mysql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr" >
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rentflix Movie Search</title>

<!-- <link rel="stylesheet" href="../../css/style.css"> -->
<link rel="stylesheet" href="resources/mytheme/css/normalize.css">
<link rel="stylesheet" href="resources/mytheme/css/stylesheet.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="jquery.jcarousel.pack.js"></script>
<script type="text/javascript" src="jquery.easing.1.3.js"></script>
<script type="text/javascript" src="jquery.jcarousel.setup.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="jquery.touchslider.min.js"></script>
<script type="text/javascript">

function removeChars(ename)
{
	var inputString = document.getElementById(ename).value;
	var c1 = replaceAll('\'','',inputString);
	document.getElementById(ename).value = replaceAll('\"','',c1);
}

function replaceAll(find, replace, str) 
{
	  return str.replace(new RegExp(find, 'g'), replace);
}
</script>

</head>
<body>
	<%
    try{
//Class.forName("com.mysql.jdbc.Driver");

 String searchFor = request.getParameter("searchBox");
 String showDescr = request.getParameter("desc");
 String sqlQuery = "select * from movies";
 String reviewQuery = "select * from reviews where movieid = (select movieid from movies where title like '" + searchFor + "')";
 String reviewQuery2 = "select review,score,firstname from reviews join users on users._z4 = reviews.userid where movieid = (select movieid from movies where title like '" + searchFor + "')";
 if(searchFor != null)
 {
	 sqlQuery += " where title like '%" + searchFor +"%'";
 }
Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

       Statement statement = connection.createStatement();
       Statement statement2 = connection.createStatement();
       Statement statement4 = connection.createStatement() ;
       Statement statement6 = connection.createStatement() ;
       Statement statement5 = connection.createStatement();

       ResultSet rs = statement.executeQuery(sqlQuery);
       ResultSet qs = statement2.executeQuery(reviewQuery2);

      
%>
<div id=header>

	<div id="logo">
		<img src="resources/mytheme/img/Logo.png" alt="RentFlix">
	</div>
	
	<div id="searchNav">
		<form name="inputForm" method="get">
			<input class="searchBox" type="text" name="searchBox" placeholder="Search for Movies..." onkeyup="fastSearch()">
			<input class="searchButton" type="submit" value="Search">
			<br>
			<!-- <input class="checkbox" type="checkbox" name="desc" value="show">Show Description -->
		</form>
	</div>
	
	<div id="navBar">
		<ul>
		    <li><a href="/MovieProject/browse">Movies</a></li>
		    <li><a href="/MovieProject/browse?genre=Family&rating=&decade=">Kids</a></li>
		    <li><a href="/MovieProject/viewqueue">Queue</a></li>
		</ul>
	</div>
	
	<div id="loginBar">
		<ul>
			<li><div id="signoutform"><form action="signout" method="post" name="loutform"><input class="lastlink" type="submit" value="Sign Out" style="text-transform:capitalize;"></form></div></li>
			<li><a class="firstlink"href="/MovieProject/MyAccount">My Account</a></li>
		</ul>
	</div>
	
</div>

  <!--  <center><h3> Selected Movie</h3></center> -->
        
	<div id="body">
	
	 <% while(rs.next()){  %>
	 
		<h1 id="MovieTitle"><%= rs.getString(2)%></h1>
		
		<div id="image">
			<img src="<%= rs.getString(8)%>" alt="some_text">
		</div>
		<div id="MovieInfo">
			<ul>
				<li><%= rs.getString(3)%></li>
				<li><%= rs.getString(4)%></li>
				<li><%= rs.getString(5)%></li>
				<li><%= rs.getString(7)%></li>
			</ul>
		</div>
		<div id="Description">
			<p><%= rs.getString(6)%></p>
		</div>
        <% } %>
        
        <h3 class="ReviewTitle">Write a Reivew</h3>
			
			<div id="ReviewTable">
				<%-- <div id="ReviewHeader">
					<ul>
						<li>User</li>
						<li>Score</li>
						<li>Comment</li>
					</ul>
				</div>
				
			   <% while(qs.next()){  %>
			   
				<div id="UserContent">
					<ul>
						<li><div class="user"><%= qs.getString(3)%></div></li>
						<li><div class="score"><%= qs.getString(2)%></div></li>
						<li><div class="comment"><p><%= qs.getString(1)%></p></div></li>
					</ul>
				</div>
				
			<% } %> --%>
			
			


<%
//**Should I input the codes here?**
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>

<%
    try{
    	
    	
    	int u = (Integer)session.getAttribute("uName");
    	//int u = (Integer)request.getSession().getAttribute("uName");
    	String user = "kyleb";
    	int i = 4;
    	
    	String searchFor = request.getParameter("searchBox");
		String movie = searchFor;
		
		//session.setAttribute("queryString", arg1);
    	request.getSession().setAttribute("queryString", "/MovieProject/movie?searchBox=" + searchFor);
    	
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

       Statement statement = connection.createStatement() ;
       Statement statement2 = connection.createStatement() ;
       
       
       
       
       ResultSet rs = statement.executeQuery("select * from movies WHERE Title='"+movie+"'") ;
       
       ResultSet rs2 = statement2.executeQuery("select * from users WHERE _z4 ="+u) ;
    
      String a = request.getParameter("searchBox");
       

   
%>
    
     
   
      <div id="writeReview">
        <form:form action="addcomment" method="post" commandName="commentForm">
            <table border="0">
                <tr>
                    <td colspan="2" align="center"><h2>Write a Review For This Movie</h2></td>
                </tr>
               <% while(rs2.next()){ %>
                <tr>
                  
                    <td><form:hidden  path="userID" value="<%=u%>"  /></td>
                </tr>
                 <% } %>
                <% while(rs.next()){ %>
                <tr>
                  
                    <td><form:hidden path="movieID" value="<%=rs.getString(1)%>"  /></td>
                  
                </tr>
                 <% } %>
                <tr>
                    <td>Score: </td>
                    <td><form:input path="score"  type="number" min="1" max="5"/></td>
                </tr>
                 <tr>
                    <td>Review: </td>
                      <td><form:textarea id="tarea" maxlength="240" path="review" style="width: 250px; height: 100px;"/></td>
                </tr>
              
                <tr>
                    <td colspan="2" align="center"><input type="submit" onfocus="removeChars('tarea')" onmouseover="removeChars('tarea')" value="Add Review"/></td>
                </tr>
              
            </table>
        </form:form>
    </div>
	
</div>
<div id="UserReviews">
		
		<a href="/MovieProject/movie?searchBox=<%= a %> ">User Reviews</a>

	</div>
<%
//**Should I input the codes here?**
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>

</div>	
<div id="footer">
			<div id="foota">
<p>Get Help</p>
<ul>
  <li><a href="#">FAQ</a></li>
  <li><a href="#">Help Center</a></li>
  <li><a href="#">Account</a></li>
  <li><a href="#">Questions</a></li>
</ul>


</div>
<div id="footb"><p>Our Company</p>
<ul>
  <li><a href="#">About us </a></li>
  <li><a href="#">Reviews  </a></li>
  <li><a href="#">Jobs  </a></li>
  <li><a href="#">Terms of Use</a></li>
</ul>

</div>
<div id="footc"><p>Getting Started</p>
<ul>
  <li><a href="#">Free Trail Offer Details  </a></li>
  <li><a href="#"> Redeem Your Gift</a></li>
</ul>

</div>
		</div>
    
</body>
</html>